<?php
namespace app\common\model;

use think\Model;
use hg\apidoc\annotation\Field;
use hg\apidoc\annotation\WithoutField;
use hg\apidoc\annotation\AddField;
use hg\apidoc\annotation\Param;

class Menu extends Model
{
    protected $table = 'menu';
    /**
     * 保存新添加的菜单
     */
    public function saveMenu($data){
        if (empty($data) || !is_array($data)) return false;
        return $this->save($data);
    }
    /**
     * 更新菜单
     */
    public function updateMenu($data){
        if (empty($data) || !is_array($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update($data);
    }
    /**
     * 修改菜单状态
     */
    public function statusMenu($data){
        if (empty($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update(['status' => $data['status']]);
    }
    /**
     * 查找所有菜单
     */
    public function getAllMenuByPid($pid, $super_user_auth)
    {
        $where = [
            ['status' ,'>' , $super_user_auth['id'] === 1 ? -20000 : -1],
            ['pid' ,'=' ,$pid]
        ];
        $order = [
            'sort'=>'asc',
            'id'=>'asc'
        ];
        return $this->where($where)->order($order)->select();
    }
    /**
     * 查找所有菜单（不包括按钮，隐藏菜单）
     */
    public function getMenuByPid($pid)
    {
        $where = [
            ['status','>' , -1],
            ['menu_type','=' ,1],
            ['pid','=' ,$pid]
        ];
        $order = [
            'sort'=>'asc',
            'id'=>'asc'
        ];
        return $this->where($where)->order($order)->select();
    }
    /**
     * 通过角色和父ID查找菜单
     */
    public function getMenuByRole($pid, $super_user_auth)
    {
        if (empty($super_user_auth)) return [];
        if ($super_user_auth['id'] === 1) {
            $where = [
                ['status' ,'=' , 1],
                ['menu_type' ,'=' , 1],
                ['pid' ,'=' ,$pid]
            ];
        } else {
            $where = [
                ['status' ,'=' , 1],
                ['menu_type' ,'=' , 1],
                ['pid' ,'=' ,$pid],
                ['menu_name', 'in', $super_user_auth['auths']]
            ];
        }
        $order = [
            'sort'=>'asc',
            'id'=>'asc'
        ];
        return $this->where($where)->order($order)->select();
    }
    /**
     * 查找所有前端路由
     */
    public function getVueRoutes($proute)
    {
        $where = [
            ['status' ,'>' , -1],
            ['menu_type' ,'=' ,1],
            ['proute' ,'=' , $proute]
        ];
        $order = [
            'sort'=>'asc',
            'id'=>'asc'
        ];
        return $this->where($where)->order($order)->select();
    }
    /**
     * 通过权限查找路由
     */
    public function getVueRouteByRole($proute, $super_user_auth)
    {
        if (empty($super_user_auth)) return [];
        if ($super_user_auth['id'] === 1) {
            $where = [
                ['status' ,'>' , -1],
                ['menu_type' ,'=' , 1],
                ['proute' ,'=' ,$proute]
            ];
        } else {
            $where = [
                ['status' ,'>' , -1],
                ['menu_type' ,'=' , 1],
                ['proute' ,'=' ,$proute],
                ['menu_name', 'in', $super_user_auth['auths']]
            ];
        }
        $order = [
            'sort'=>'asc',
            'id'=>'asc'
        ];
        return $this->where($where)->order($order)->select();
    }
    /**
     * 通过标题查找菜单
     */
    public function getMenuByTitle($title)
    {
        if (empty($title)) return [];
        $where = [
            ['status' ,'>' , -1],
            ['title' ,'=' , $title]
        ];
        return $this->where($where)->find();
    }
    /**
     * 通过名称查找菜单
     */
    public function getMenuByName($menuName)
    {
        if (empty($menuName)) return [];
        $where = [
            ['status' ,'>' , -1],
            ['menu_name' ,'=' , $menuName]
        ];
        return $this->where($where)->find();
    }
}
